package com.lyj.ui.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import com.lyj.util.dao.DbHelper;

@WebFilter("/TransFilter")
public class TransFilter implements Filter {
	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// 开启事务、放行、提交事务、关闭数据库链接
		try {
			DbHelper.beginTransaction();
			chain.doFilter(request, response);
			DbHelper.commitTransaction();
			DbHelper.close();
		} catch (Exception e) {
			try {
				DbHelper.rollbackTransaction();
			} catch (Exception e1) {
				throw new RuntimeException(e1);
			}
			throw new RuntimeException(e);
		}
	}

	public void init(FilterConfig fConfig) throws ServletException {

	}

}
